import Vue from 'vue'
import VueRouter from 'vue-router'
import routes from './router.js'
import _self from '../utils'
import store from '@/store'


const router = new VueRouter({
    mode: 'hash',
    base: '/office/',
    routes
});
Vue.use(VueRouter)


let loginlist = []//登录模块可以直接跳转
let cxlist = ['/indexcx', '/cxxq', '/jbxq', '/bgxq', '/zjxq']//查询模块可以不登录跳转
let querylist = ['/cxxq', '/jbxq', '/bgxq', '/zjxq']//携带参数的页面(未登录时的)
let lqlist = [] //携带参数的页面(登录时的)
// console.log(store.state)
store.state.hymenulist.forEach((item, index) => {
    item.menulist.forEach((element, index) => {
        if (element.list) {
            lqlist = [...element.list, ...lqlist]
        }
    })
})
//  console.log(lqlist)
routes.forEach((item, index) => {
    // console.log(item)

    if (item.path == '/login' && item.children) {
        loginlist.push(item.path)
        item.children.forEach((ele, i) => {
            loginlist.push(ele.path)
        })
    }
    if (item.path == '/logingr' && item.children) {
        loginlist.push(item.path)
        item.children.forEach((ele, i) => {
            loginlist.push(ele.path)
        })
    }
    if (item.path == '/loginjg' && item.children) {
        loginlist.push(item.path)
        item.children.forEach((ele, i) => {
            loginlist.push(ele.path)
        })
    }
    //
    if (item.path == '/indexcx' && item.children) {
        cxlist.push(item.path)
        item.children.forEach((ele, i) => {
            cxlist.push(ele.path)
        })
    }
})


// console.log('+++++',jgtoken,grtoken);
router.beforeEach((to, from, next) => {

    // console.log(to, from,)
    if (to.meta.title) {//判断是否有标题
        document.title = to.meta.title + ' - ' + '鲁土协会'
    }

    let zjtoken = localStorage.getItem('zjtoken');

    let jgtoken = localStorage.getItem('jgtoken');
    // console.log(jgtoken)
    let grtoken = localStorage.getItem('grtoken');
    // console.log(to, from)
    if (to.query.label) {
        localStorage.setItem("label", to.query.label);
    }
    let label = localStorage.getItem('label');
    if (loginlist.indexOf(to.path) != -1) {
        next();//登录模块
    } else if (cxlist.indexOf(to.path) != -1) {//可未登录直接跳转
        if (querylist.indexOf(to.path) != -1 && _self.pdobj(JSON.parse(JSON.stringify(to.query)))) {//携带参数的页面如果未带参数拦截（不需要登录）
            // console.log(to.query, '111', _self.pdobj(to.query), from)
            next('/indexcx')//(截断)
        } else {
            next()
        }
    } else if (lqlist.indexOf(to.name) != -1 && _self.pdobj(JSON.parse(JSON.stringify(to.query)))) {//携带参数的页面如果未带参数拦截(需要登录的)
        // console.log(to.query)
        if (jgtoken) {
            next('/indexhy')//(截断)
        }
        else if (grtoken) {
            next('/indexgr')//(截断)
        } else if (zjtoken) {
            next('/indexzj')//(截断)
        } else {
            next('/login');
        }
    } else if (!grtoken && !jgtoken && !zjtoken) {//页面未登录,不可跳转
        // next('/login');
        if (label) {
            if (to.path == '/grkclb') {
                next('/gelogingr');
            } else if (to.path == '/hyzxpj') {
                next('/geloginjg');
            } else {
                next('/login');//删掉
            }
        } else {
            next('/login');
        }
    } else if (!grtoken && _self.findValue(routes[2], to.path)) {
        if (label) {
            if (to.path == '/grkclb') {
                next('/gelogingr');
            } else if (to.path == '/hyzxpj') {
                next('/geloginjg');
            } else {
                next('/login');//删掉
            }
        } else {
            next('/gelogin');
        }
    } else if (!jgtoken && _self.findValue(routes[4], to.path)) {
        // console.log(_self.findValue(routes[4], to.path))
        
        if (label) {
            console.log(1)
            if (to.path == '/grkclb') {
                next('/gelogingr');
            } else if (to.path == '/hyzxpj') {
                next('/geloginjg');
            } else {
                next('/login');//删掉
            }
        } else {
            next('/jglogin');
        }
    } else if (!zjtoken && _self.findValue(routes[6], to.path)) {
        // console.log(_self.findValue(routes[6], to.path))
        if (label) {
            if (to.path == '/grkclb') {
                next('/gelogingr');
            } else if (to.path == '/hyzxpj') {
                next('/geloginjg');
            } else {
                next('/login');//删掉
            }
        } else {
            next('/zjlogin');
        }
    } else {
        next()
    }
});

export default router